﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Dotnet.Code
{
    public static partial class Ext
    {
        /// <summary>
        /// URL字符串编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEnCode(this string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString().ToUpper().Trim());
        }



        public static string[] Add(this string[] str,string[] str2)
        {
            List<string> list1 = str.ToList<string>();
            List<string> list2 = str2.ToList<string>();
            list2.ForEach(t=>{
                list1.Add(t);
            });
            return list1.ToArray<string>();
        }

        public static bool IsTimeString(this string str)
        {
            try
            {
                DateTime.Parse(str.Trim());
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool IsMoneyString(this string str)
        {
            try
            {
                Decimal.Parse(str.Trim().Trim(new char[] { '¥','$'}));
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 提取字符串的中文汉字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ChineseString(this string str)
        {
            Regex reg = new Regex("[\u4e00-\u9fa5]+");
            StringBuilder sb = new StringBuilder();
            foreach (Match v in reg.Matches(str))
            {
                sb.Append(v.Value);
            }
            return sb.ToString().Trim();
        }

        public static bool IsChineseChar(this char ch)
        {
            Regex reg = new Regex("[\u4e00-\u9fa5]+");
            if (string.IsNullOrEmpty(ch.ToString().Trim()) == true)
                return true;
            return reg.IsMatch(ch.ToString().Trim());
        }


        public static DateTime UnixStringToDateTime(this string str)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            int result = 0;
            if (str.Length > 10)
            {
                str = str.Substring(0, 10);    
            }
            bool ret = Int32.TryParse(str, out result);
            if (ret == true)
            {
                dtDateTime = dtDateTime.AddSeconds(result);

            }
            return dtDateTime.ToLocalTime();
        }
        public static string UnixStringToDateStr(this string str)
        {
            return UnixStringToDateTime(str).ToString("yyyy-MM-dd");
        }
        public static string UnixStringToDateTimeStr(this string str)
        {
            return UnixStringToDateTime(str).ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 比较两个字符串的相似度比率值。
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <param name="destStr"></param>
        /// <returns></returns>

        public static double StringSameRate(this string sourceStr,string destStr,out string diffStr)
        {
            string source = sourceStr.ChineseString();
            string dest = destStr.ChineseString();
            if(source.Length-dest.Length>5 || source.Length - dest.Length < -5)
            {
                diffStr = "";
                return 0.0;
            }
            double sameCount = 0;
            StringBuilder sb = new StringBuilder();
            int index = 0;
            foreach(char ch in source)
            {
                index++;
                if(dest.Contains(ch.ToString())==true)
                {
                    sameCount++;
                }
                else
                {
                    sb.AppendFormat("{0}:{1}",index,ch);
                }
            }
            diffStr =string.Format("差异:{0}",sb.ToString().Trim());
            return Math.Round(sameCount*100 / (double)source.Length,2);
        }
    }
}
